#!/usr/bin/perl
package ConfDatabase;
use Mojo::Base -base;
use DBI;
use feature qw/switch/;
use DBIx::Custom;

has 'encoding' => 'utf-8'; # в чем БД отдает и принимает данные
has 'database' => 'test';
has 'hostname' => 'localhost';
has 'port' => '';
has 'dbd' => 'mysql'; # SQLite || mysql
has 'user' => 'root';
has 'password' => '';
#has 'dbname' => '/home/dima/Рабочий стол/Mojo-testing/scool_life/db/scoollife.db'; # for sqlite3


#-------------------------- dsn ----------- 
sub dsn {
 my $se = shift;
 my $rv;
 given ( $se->dbd ){
  when (/mysql/i){ # for mysql
   $rv = "DBI:".$se->dbd.":database=".$se->database.";host=".$se->hostname.";port=".$se->port;
  }
  when (/sqlite/i){ # for sqlite3
   $rv = "dbi:".$se->dbd."dbname=".$se->dbname;
  }
  # for other database add 'when (...' here
  #...
  default { 'dsn!' }
 }
 return $rv; 
};
# --------------------------------------------

sub connect {
 my $se = shift;
 my $dbi;
 eval {
  $dbi = DBIx::Custom->connect(
    dsn => $se->dsn, 
    user => $se->user, 
    password => $se->password,
  ); 
 };
 if ( !$dbi or $@ ){
    warn "Can't connect: $@";
 }
 return $dbi;
};



1;